package com.primb.bench.project.home.presenter;

import android.content.Context;

import com.primb.bench.R;
import com.primb.bench.common.constant.AppConstant;
import com.primb.bench.common.dialog.SelectListDialog;
import com.primb.bench.common.override.HttpSubscriber;
import com.primb.bench.project.home.contract.AddInfoContract;
import com.primb.bench.project.home.entity.ClientInfoEntity;
import com.primb.bench.project.home.entity.EnsureInfoEntity;
import com.primb.bench.project.home.entity.HttpStrEntity;
import com.primb.bench.project.home.entity.KeyEntity;
import com.primb.bench.project.home.entity.LoanInfoEntity;
import com.primb.bench.project.home.entity.LogoInfoEntity;
import com.primb.bench.project.home.entity.TaxInfoEntity;
import com.primb.bench.project.home.model.AddInfoModelImpl;
import com.primb.netlibs.exception.ApiException;

import java.util.ArrayList;
import java.util.List;

import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

import static android.R.attr.type;

/**
 * Created by Administrator on 2017/05/04
 */

public class AddInfoPresenterImpl implements AddInfoContract.Presenter {

    private AddInfoModelImpl mModel;
    private AddInfoContract.View mView;
    private CompositeSubscription subscription;
    /**
     * 定价信息
     */
    public LogoInfoEntity logoInfoEntity;
    /**
     * 客户基本信息
     */
    public ClientInfoEntity clientInfoEntity;

    /**
     * 贷款基本信息
     */
    public LoanInfoEntity loanInfoEntity;

    /**
     * 担保信息
     */
    public EnsureInfoEntity ensureInfoEntity;

    /**
     * 税收信息
     */
    public TaxInfoEntity taxInfoEntity;

    private List<String> pickerList = new ArrayList<>();

    private String clientType;
    private Context context;
    private String priceId;

    public AddInfoPresenterImpl(Context context, AddInfoContract.View mView) {
        this.mView = mView;
        this.context = context;
        this.mModel = new AddInfoModelImpl();
        logoInfoEntity = new LogoInfoEntity();
        subscription = new CompositeSubscription();
        clientInfoEntity = new ClientInfoEntity();
        loanInfoEntity = new LoanInfoEntity();
        loanInfoEntity.setLoanDateUnit("年");
        ensureInfoEntity = new EnsureInfoEntity();
        taxInfoEntity = new TaxInfoEntity();
    }

    @Override
    public void handleInfoForList(int code, KeyEntity data) {
        switch (code) {
            case AppConstant.ADD_CUSTOMER_TYPE:
                clientInfoEntity.setCustomerType(data.getKey());
                break;
            case AppConstant.ADD_CUSTOMER_CREDIT:
                clientInfoEntity.setCustomerCredit(data.getKey());
                break;
            case AppConstant.ADD_CUSTOMER_SIZE:
                clientInfoEntity.setCustomerSize(data.getKey());
                break;
            case AppConstant.ADD_LOAN_TYPE:
                loanInfoEntity.setLoanType(data.getKey());
                loanInfoEntity.setLoanTypeId(data.getValue());
                break;
            case AppConstant.ADD_LOAN_REPAYMENT_TYPE:
                loanInfoEntity.setLoanRepayment(data.getKey());
                loanInfoEntity.setLoanRepaymentId(data.getValue());
                break;
            case AppConstant.ADD_LOAN_DATE:
                loanInfoEntity.setLoanDateUnit(data.getKey());
                break;
            case AppConstant.ADD_LOAN_BUSINESS_TYPE:
                loanInfoEntity.setLoanBusinessType(data.getKey());
                loanInfoEntity.setLoanBusinessTypeId(data.getValue());
                break;
            case AppConstant.ADD_LOAN_INDUSTRY:
                loanInfoEntity.setLoanIndustry(data.getKey());
                loanInfoEntity.setLoanIndustryId(data.getValue());
                break;
            case AppConstant.ADD_LOAN_RATE_TYPE:
                loanInfoEntity.setLoanRateType(data.getKey());
                loanInfoEntity.setLoanRateTypeId(data.getValue());
                break;
            case AppConstant.ADD_ENSURE_MAIN_TYPE:
                if (!data.getKey().equals(ensureInfoEntity.getEnsureMainType())) {
                    deleteAllEnsure(ensureInfoEntity);
                }
                ensureInfoEntity.setEnsureMainType(data.getKey());
                ensureInfoEntity.setEnsureMainTypeId(data.getValue());
                ensureInfoEntity.setEnsureConcretenessType("");
                ensureInfoEntity.setEnsureConcretenessTypeId("");
                break;
            case AppConstant.ADD_ENSURE_CONCRETENESS:
                ensureInfoEntity.setEnsureConcretenessType(data.getKey());
                ensureInfoEntity.setEnsureConcretenessTypeId(data.getValue());
                break;
        }
    }

    private void deleteAllEnsure(EnsureInfoEntity ensureInfoEntity) {
        if (!"0".equals(ensureInfoEntity.getCommonGuarantee())
                || !"0".equals(ensureInfoEntity.getHighGuarantee())
                || !"0".equals(ensureInfoEntity.getLnAcctRecvAmt())
                || !"0".equals(ensureInfoEntity.getLnFinImpawnAmt())
                || !"0".equals(ensureInfoEntity.getLnOthMortAmt())
                || !"0".equals(ensureInfoEntity.getLnPropertyAmt())) {
            mView.showLoading();
            Subscription subscribe = mModel.deleteAllEnsureObject(priceId).subscribe(new HttpSubscriber<HttpStrEntity>(context) {
                @Override
                public void onCompleted() {
                    mView.hideLoading();
                }

                @Override
                public void onNext(HttpStrEntity o) {
                    if (o != null && o.isSuccess()) {
                        ensureInfoEntity.setLnAcctRecvAmt("0");
                        ensureInfoEntity.setLnOthMortAmt("0");
                        ensureInfoEntity.setLnFinImpawnAmt("0");
                        ensureInfoEntity.setLnPropertyAmt("0");
                        ensureInfoEntity.setHighGuarantee("0");
                        ensureInfoEntity.setCommonGuarantee("0");
                        mView.setEnsureEntityToView(ensureInfoEntity);
                    }
                }

                @Override
                public void onError(ApiException e) {
                    mView.showError(e.getMessage());
                    mView.hideLoading();
                }
            });
            subscription.add(subscribe);
        }
    }

    @Override
    public void getPickerList(int code, String type) {
        SelectListDialog selectListDialog = mView.showPickerDialog(code);
        Subscription subscribe = mModel.getPickerList(code, type).subscribe(new HttpSubscriber<List<KeyEntity>>(context) {
            @Override
            public void onError(ApiException e) {
                mView.showError(e.getMessage());
                selectListDialog.dismiss();
            }

            @Override
            public void onCompleted() {
            }

            @Override
            public void onNext(List<KeyEntity> entityList) {
                selectListDialog.setDateList(entityList);
            }
        });
        subscription.add(subscribe);
    }

    @Override
    public boolean isCheckEmpty(int requestCode) {
        switch (requestCode) {
            case AppConstant.LOAN_CUSTOMER_REQUEST:
                mView.setDataToClientEntity(clientInfoEntity);
                if (clientInfoEntity.getCustomerAdjustRate() != null && !clientInfoEntity.getCustomerAdjustRate().equals("") && clientInfoEntity.getCustomerSize() != null
                        && clientInfoEntity.getCustomerName() != null && !clientInfoEntity.getCustomerName().equals("")
                        && clientInfoEntity.getCustomerCredit() != null && clientInfoEntity.getCustomerType() != null) {
                    clientInfoEntity.setCustomerInfoEmpty(false);
                }
                return clientInfoEntity.isCustomerInfoEmpty();
            case AppConstant.LOAN_LOAN_REQUEST:
                mView.setDataToLoanEntity(loanInfoEntity);
                if (loanInfoEntity.getLoanType() != null
                        && loanInfoEntity.getLoanBusinessType() != null && loanInfoEntity.getLoanDate() != null
                        && !loanInfoEntity.getLoanDate().equals("") && loanInfoEntity.getLoanDateUnit() != null
//                        && loanInfoEntity.getLoanRepaymentId() != null && loanInfoEntity.getLoanRepayment() != null
                        && loanInfoEntity.getLoanMoney() != null) {
                    loanInfoEntity.setLoanInfoEmpty(false);
                }
                return loanInfoEntity.isLoanInfoEmpty();
            case AppConstant.LOAN_ENSURE_REQUEST:
                if (ensureInfoEntity.getEnsureMainType() != null && !ensureInfoEntity.getEnsureMainType().equals("")) {
                    if (ensureInfoEntity.getEnsureMainType().equals("保证") && ensureInfoEntity.getEnsureConcretenessType() != null && !ensureInfoEntity.getEnsureConcretenessType().equals("")) {
                        ensureInfoEntity.setEnsureInfoEmpty(false);
                        return ensureInfoEntity.isEnsureInfoEmpty();
                    } else if (!ensureInfoEntity.getEnsureMainType().equals("保证")) {
                        ensureInfoEntity.setEnsureInfoEmpty(false);
                        return ensureInfoEntity.isEnsureInfoEmpty();
                    } else {
                        return ensureInfoEntity.isEnsureInfoEmpty();
                    }
                } else {
                    return ensureInfoEntity.isEnsureInfoEmpty();
                }
            case AppConstant.LOAN_TAX_REQUEST:
                mView.setDataToTaxEntity(taxInfoEntity);
                return false;
        }
        return true;
    }

    public Object getInfoEntity(int requestCode) {
        switch (requestCode) {
            case AppConstant.LOAN_CUSTOMER_REQUEST:
                return clientInfoEntity;
            case AppConstant.LOAN_LOAN_REQUEST:
                return loanInfoEntity;
            case AppConstant.LOAN_ENSURE_REQUEST:
                return ensureInfoEntity;
            case AppConstant.LOAN_TAX_REQUEST:
                return taxInfoEntity;
        }
        return null;
    }

    @Override
    public void onViewClick(int viewId, int requestCode) {
        switch (viewId) {
            case R.id.iv_top:
                if (!isCheckEmpty(requestCode)) {
                    mView.setResultToCalculate(requestCode, getInfoEntity(requestCode));
                } else {
                    mView.showError("请完善信息后保存");
                }
                break;
            case R.id.tv_customer_type:
//                getPickerList(AppConstant.ADD_CUSTOMER_TYPE, "");
                break;
            case R.id.iv_customer_credit:
            case R.id.tv_customer_credit:
                getPickerList(AppConstant.ADD_CUSTOMER_CREDIT, "");
                break;
//            case R.id.tv_customer_size:
//                getPickerList(AppConstant.ADD_CUSTOMER_SIZE, "");
//                break;
            case R.id.tv_loan_type:
                getPickerList(AppConstant.ADD_LOAN_TYPE, "");
                break;
            case R.id.tv_loan_repayment_type:
                getPickerList(AppConstant.ADD_LOAN_REPAYMENT_TYPE, "");
                break;
            case R.id.tv_condition_date_unit:
                getPickerList(AppConstant.ADD_LOAN_DATE, "");
                break;
            case R.id.tv_loan_business_type:
                getPickerList(AppConstant.ADD_LOAN_BUSINESS_TYPE, clientType);
                break;
            case R.id.tv_loan_industry:
                getPickerList(AppConstant.ADD_LOAN_INDUSTRY, "");
                break;
            case R.id.tv_loan_rate_type:
                getPickerList(AppConstant.ADD_LOAN_RATE_TYPE, "");
                break;
            case R.id.tv_ensure_main_type:
                getPickerList(AppConstant.ADD_ENSURE_MAIN_TYPE, "");
                break;
            case R.id.tv_ensure_info:
                mView.getEnsureInfo(AppConstant.ENSURE_OBJECT_INFO);
                break;
            case R.id.tv_ensure_concreteness_type:
                if (ensureInfoEntity.getEnsureMainTypeId() != null && !ensureInfoEntity.getEnsureMainTypeId().equals(""))
                    getPickerList(AppConstant.ADD_ENSURE_CONCRETENESS, ensureInfoEntity.getEnsureMainTypeId());
                else
                    mView.showError("请选择担保方式大类");
                break;
        }
    }

    @Override
    public void setClientType(String clientType) {
        this.clientType = clientType;
    }

    @Override
    public void setPriceId(String priceId) {
        this.priceId = priceId;
    }

    @Override
    public void start() {
        mView.showLayoutForType();
    }

    @Override
    public void onRecycle() {
        subscription.clear();
    }
}